﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Transactions;
using WpfSnqkGasAnalysis.Common;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis.IBusiness_impl
{
    /// <summary>
    /// 天然气，业务处理
    /// </summary>
    public sealed class Gas_analysisBusiness : BaseBusiness
    {
        readonly IGas_analysisDAL gas_AnalysisDAL = ServicesHelpter.GetService<IGas_analysisDAL>();
        readonly IGas_check_historyDAL gas_Check_HistoryDAL = ServicesHelpter.GetService<IGas_check_historyDAL>();
        readonly IOperate_logDAL operate_LogDAL = ServicesHelpter.GetService<IOperate_logDAL>();
        readonly IGas_analysis_upload_logDAL gas_Analysis_Upload_LogDAL = ServicesHelpter.GetService<IGas_analysis_upload_logDAL>();
        readonly ISource_dictDAL source_DictDAL = ServicesHelpter.GetService<ISource_dictDAL>();


        public IResult Add(Gas_analysis gas_Analysis)
        {
            //参数检查
            var resultParame = ParamesCheck(gas_Analysis);
            if (resultParame.CodeEnum != ResultCode.Success)
            {
                return resultParame;
            }

            //验证编号是否重复  //添加时间：2023-1-10 14:35:41
            bool right = gas_AnalysisDAL.CheckNo(gas_Analysis.Sample_no);
            if (right)
            {
                return new Result("样品编号重复了");
            }

            //创建时间：2023-1-10 14:44:57。
            if (gas_Analysis.All_calc_value < 99 || gas_Analysis.All_calc_value > 101)
            {
                return new Result("总值范围准许在99~101 ，请检查");
            }

            gas_Analysis.Input_real_name = CurrentLoginUser.User_real_name;
            gas_Analysis.Input_user_id = CurrentLoginUser.Id;
            gas_Analysis.Input_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

            var result = gas_AnalysisDAL.Add(gas_Analysis);
            return result;
        }

        /// <summary>
        /// 参数验证检查
        /// </summary>
        /// <param name="gas_Analysis"></param>
        /// <returns></returns>
        /// 创建时间：2023-1-5 20:07:30。作者：王浩力
        private static IResult ParamesCheck(Gas_analysis gas_Analysis)
        {
            //参数检查，//添加时间 2023-1-4 16:51:30
            if (string.IsNullOrWhiteSpace(gas_Analysis.Cate))
            {
                return new Result("类别不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Sample_no))
            {
                return new Result("编号不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Well_no))
            {
                return new Result("井号不可为空");
            }

            //需求调整，客户允许为空。//修改时间：2023-1-17 11:15:10。
            //if (string.IsNullOrWhiteSpace(gas_Analysis.Build_name))
            //{
            //    return new Result("构造名称不可为空");
            //}
            //if (string.IsNullOrWhiteSpace(gas_Analysis.Top_horizon))
            //{
            //    return new Result("顶界层位不可为空");
            //}
            //if (string.IsNullOrWhiteSpace(gas_Analysis.Bottom_horizon))
            //{
            //    return new Result("底界层位不可为空");
            //}

            if (string.IsNullOrWhiteSpace(gas_Analysis.Sampling_location))
            {
                return new Result("取样位置不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Samping_company_name))
            {
                return new Result("取样单位不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Analysis_company_name))
            {
                return new Result("分析单位不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Samping_condition))
            {
                return new Result("取样条件不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Client))
            {
                return new Result("委托单位不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Samping_time))
            {
                return new Result("取样时间不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Analysis_time))
            {
                return new Result("分析时间不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Samping_user))
            {
                return new Result("取样人不可为空");
            }
            if (string.IsNullOrWhiteSpace(gas_Analysis.Analysis_user))
            {
                return new Result("分析人不可为空");
            }
            //if (string.IsNullOrWhiteSpace(gas_Analysis.Samping_temperature))
            //{
            //    return new Result("取样温度不可为空");
            //}
            if (gas_Analysis.O2 == 0)
            {
                return new Result("请输入指标");
            }

            return new ResultSuccess();
        }

        /// <summary>
        /// 确认校对
        /// </summary>
        /// <param name="gas_Analysis"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-9 11:17:12。作者：王浩力
        public IResult ConfirmProofread(Gas_analysis gas_Analysis)
        {
            //参数检查
            var resultParame = ParamesCheck(gas_Analysis);
            if (resultParame.CodeEnum != ResultCode.Success)
            {
                return resultParame;
            }

            //添加时间：2023-1-10 14:38:31
            var info = Get(gas_Analysis.Id);
            if (info == null)
            {
                return new Result("没找到原记录");
            }

            //验证编号是否重复  //添加时间：2023-1-10 14:35:41
            if (info.Sample_no != gas_Analysis.Sample_no)
            {
                bool right = gas_AnalysisDAL.CheckNo(gas_Analysis.Sample_no);
                if (right)
                {
                    return new Result("样品编号重复了");
                }
            }

            //创建时间：2023-1-10 14:44:57。
            if (gas_Analysis.All_calc_value < 99 || gas_Analysis.All_calc_value > 101)
            {
                return new Result("总值范围准许在99~101 ，请检查");
            }

            gas_Analysis.Proofreader_userId = CurrentLoginUser.Id;
            gas_Analysis.Proofreader = CurrentLoginUser.User_real_name;
            gas_Analysis.Proofread_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            gas_Analysis.O_step = 2;
            var result = gas_AnalysisDAL.Update(gas_Analysis, true);
            //记录校对修改日志
            if (result.Code == (int)ResultCode.Success)
            {
                result.Msg = "校对成功";
            }
            return result;
        }

        /// <summary>
        /// 驳回处理，审核处理。扩展字段E3传驳回理由
        /// </summary>
        /// <param name="gas_Analysis"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-10 14:12:02。作者：王浩力
        public IResult CheckGas(Gas_analysis gas_Analysis)
        {
            byte stat = 1;
            if (gas_Analysis.O_step == 3)
            {
                //驳回
                stat = 0;
                if (string.IsNullOrWhiteSpace(gas_Analysis.E3))
                {
                    return new Result("驳回理由不可为空");
                }
            }
            else if (gas_Analysis.O_step == 4)
            {
                //通过
                stat = 1;
            }
            //记录审核历史
            Gas_check_history history = new Gas_check_history()
            {
                Gas_analysis_key = gas_Analysis.Id,
                Check_stat = stat,
                Check_user_id = CurrentLoginUser.Id,
                Check_user_name = CurrentLoginUser.User_real_name,
                Reject_reason = gas_Analysis.E3,
            };
            //事务处理
            var result = gas_Check_HistoryDAL.AddCheckHandleTrasacation(gas_Analysis, history);
            if (result.Code == 200)
            {
                //生成PDF文件到本地保存  //添加时间：2023-1-10 10:48:10。
                string path = MyConfigReader.GetConfigValue("proocessPassPdfFolderPath");
                if (!string.IsNullOrWhiteSpace(path))
                {
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                }

                return new ResultSuccess("审核处理成功");
            }
            return result;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="condiction"></param>
        /// <param name="p"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-7 16:40:14。作者：王浩力
        public PageModel<Gas_analysis> Page(Gas_analysis condiction, int p, int rows)
        {
            if (CurrentLoginUser.Role_id == "校对人")
            {
                if (condiction.O_step.HasValue)
                {
                    condiction.WorkList = new List<int>() { (int)condiction.O_step };
                }
                else
                {
                    condiction.WorkList = new List<int>() { 1, 3 };
                }
            }
            else if (CurrentLoginUser.Role_id == "审核人")
            {
                if (condiction.O_step.HasValue)
                {
                    condiction.WorkList = new List<int>() { (int)condiction.O_step };
                }
                else
                {
                    condiction.WorkList = new List<int>() { 2 };
                }
            }
            else
            {
                if (condiction.O_step.HasValue)
                {
                    condiction.WorkList = new List<int>() { (int)condiction.O_step };
                }
            }

            return gas_AnalysisDAL.Page(condiction, p, rows);
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        /// <param name="physicalDelete">是否物理删除</param>
        /// <returns></returns>
        /// 创建时间：2022-12-7 21:13:27。作者：王浩力
        public Result Delete(long id)
        {
            var old = gas_AnalysisDAL.Get(id);
            if (old == null)
            {
                return new Result("没找到要删除的记录");
            }
            //检查是否有校对记录
            var upList = gas_Analysis_Upload_LogDAL.GetList(id);
            if (upList?.Count > 0)
            {
                return new Result("记录被引用，不可删除");
            }

            //检查是否有审核记录
            var checkList = gas_Check_HistoryDAL.GetList(id);
            if (checkList?.Count > 0)
            {
                return new Result("记录被引用，不可删除");
            }
            Result result = gas_AnalysisDAL.Delete(id);
            if (result.Code == 200)
            {
                //记录操作日志
                CurrentLoginUser.E12 = 16;
                CurrentLoginUser.E3 = $"删除天然气记录，id={id} ";
                operate_LogDAL.AddOperateLog(CurrentLoginUser);
            }
            return result;
        }

        /// <summary>
        /// Gas_analysis 根据主键查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-8 23:27:47。作者：王浩力
        public Gas_analysis Get(long id)
        {
            return gas_AnalysisDAL.Get(id);
        }

        /// <summary>
        /// 将相关的id替换为文本
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 创建时间：2022-12-13 11:54:12。作者：王浩力
        public GasPrintView GetInfoStr(long id)
        {
            var info2 = gas_AnalysisDAL.Get(id);

            var emp = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<Gas_analysis, GasPrintView>();
            var info = emp.Map(info2);

            //类别
            info.Cate = source_DictDAL.Get((long)info.Cate.ToMyDecimal())?.Item_name;

            //构建名称
            info.Build_name = source_DictDAL.Get((long)info.Build_name.ToMyDecimal())?.Item_name;

            //取样位置，覆盖为文本  //修改时间：2023-1-15 15:59:26
            //info.Sampling_location = source_DictDAL.Get((long)info.Sampling_location.ToMyDecimal())?.Item_name;
            if (Regex.IsMatch(info.Sampling_location, "^\\d{19}"))
            {
                info.Sampling_location = source_DictDAL.Get((long)info.Sampling_location.ToMyDecimal())?.Item_name;
            }
            else
            {
                info.Sampling_location = info.Sampling_location;
            }


            //层位  //修改时间：2023-1-11 13:29:11；打印只显示符号
            //info.Top_horizon = source_DictDAL.Get((long)info.Top_horizon.ToMyDecimal())?.Item_name;
            //info.Bottom_horizon = source_DictDAL.Get((long)info.Bottom_horizon.ToMyDecimal())?.Item_name;
            info.Top_horizon = source_DictDAL.Get((long)info.Top_horizon.ToMyDecimal())?.Dict_code;
            info.Bottom_horizon = source_DictDAL.Get((long)info.Bottom_horizon.ToMyDecimal())?.Dict_code;

            info.Client = source_DictDAL.Get((long)info.Client.ToMyDecimal())?.Item_name;
            info.Samping_condition = source_DictDAL.Get((long)info.Samping_condition.ToMyDecimal())?.Item_name;
            info.Samping_company_name = source_DictDAL.Get((long)info.Samping_company_name.ToMyDecimal())?.Item_name;
            info.Analysis_company_name = source_DictDAL.Get((long)info.Analysis_company_name.ToMyDecimal())?.Item_name;

            //  info.Samping_user = source_DictDAL.Get((long)info.Samping_user.ToMyDecimal())?.Item_name;
            // info.Analysis_user = source_DictDAL.Get((long)info.Analysis_user.ToMyDecimal())?.Item_name;

            var userList = new UserBusiness().GetAllUser();

            //分析人，电子签名
            info.E6 = userList.FirstOrDefault(g => g.Id == info.Analysis_user_id)?.SignatureImgPath;

            //校对人，电子签名
            if (info.Proofreader_userId.HasValue && info.Proofreader_userId > 0)
            {
                info.E7 = userList.FirstOrDefault(g => g.Id == info.Proofreader_userId)?.SignatureImgPath;
            }

            //审核人，电子签名
            var history = gas_Check_HistoryDAL.GetList(id);
            if (history?.Count > 0)
            {
                var find = history.OrderByDescending(g => g.Id).FirstOrDefault();
                if (find != null)
                {
                    var ckUser = userList.FirstOrDefault(g => g.Id == find.Check_user_id);
                    info.E5 = ckUser?.User_real_name;//审核人姓名
                    info.E8 = ckUser?.SignatureImgPath; //审核人电子签名图片url
                }
            }

            /**
              默认未脱硫，使用上一部分公式计算；
            脱硫的打印值：是使用未脱硫的值，再次带入脱硫的公式计算；
             */

            //计算打印的数据  //添加时间：2023-1-10 12:59:38。
            //9.硫化氢.
            //硫化氢（百分浓度）=(硫化氢mg*(11.88/17.04)/10000)*1000+0.5)/1000;
            GasPrintView zbView = new GasPrintView();
            zbView.H2Smg = (info.H2Smg * (11.88M / 17.04M)) / 10000M;

            //打印的氮：
            //1.空气中的氮=录入的氧* 3.7287
            //2. 氮=录入的氮-1.算出来的氮
            //3. 打印的氮=2.氮*((100/(100-(录入的氧气+1.空气中的氮))
            decimal N2_step1 = Convert.ToDecimal(info.O2) * 3.7287M;
            decimal N2_step2 = Convert.ToDecimal(info.N2) - N2_step1;
            decimal baiChu100AND_O2 = 100m / (100m - (Convert.ToDecimal(info.O2) + N2_step1));
            zbView.N2 = N2_step2 * baiChu100AND_O2;

            //氦 He   mol%
            //氦=录入的氦*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.He = info.He * baiChu100AND_O2;

            //氢 H2    mol%
            //氢=录入的氢*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.H2 = info.H2 * baiChu100AND_O2;

            //碳六加   mol%
            //碳六加=录入的C6*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.C6 = info.C6 * baiChu100AND_O2;

            //丙烷   mol%
            //丙烷C3=录入的C3*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.C3 = info.C3 * baiChu100AND_O2;

            //异丁烷    mol%
            //iC4=录入的iC4*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.Ic4 = info.Ic4 * baiChu100AND_O2;

            //正丁烷   mol%
            //nC4=录入的nC4*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.Nc4 = info.Nc4 * baiChu100AND_O2;

            //异戊烷 mol%
            //iC5=录入的iC5*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.Ic5 = info.Ic5 * baiChu100AND_O2;

            //正戊烷 mol%
            //nC5=录入的nC5*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.Nc5 = info.Nc5 * baiChu100AND_O2;

            //二氧化碳 mol%
            //CO2=录入的CO2*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.E2 = (decimal)info.CO2 * baiChu100AND_O2;

            //乙烷
            //C2=录入的C2*((100/(100-(录入的氧气+1.空气中的氮))
            zbView.C2 = info.C2 * baiChu100AND_O2;

            //处理小数位，保留三位  //修改时间：2023-1-12 16:38:17
            decimal deal_N2 = Math.Round(Convert.ToDecimal(zbView.N2), 3);
            decimal deal_He = Math.Round(Convert.ToDecimal(zbView.He), 3);
            decimal deal_H2 = Math.Round(Convert.ToDecimal(zbView.H2), 3);
            decimal deal_C6 = Math.Round(Convert.ToDecimal(zbView.C6), 3);

            decimal deal_C3 = Math.Round(Convert.ToDecimal(zbView.C3), 3);
            decimal deal_Ic4 = Math.Round(Convert.ToDecimal(zbView.Ic4), 3);
            decimal deal_Nc4 = Math.Round(Convert.ToDecimal(zbView.Nc4), 3);

            decimal deal_Ic5 = Math.Round(Convert.ToDecimal(zbView.Ic5), 3);
            decimal deal_Nc5 = Math.Round(Convert.ToDecimal(zbView.Nc5), 3);
            decimal deal_E2 = Math.Round(zbView.E2, 3);
            decimal deal_C2 = Math.Round(Convert.ToDecimal(zbView.C2), 3);

            //甲烷 C1（未脱硫）
            //zbView.C1 = 100 - zbView.N2 - zbView.He - zbView.H2 - zbView.C6 - zbView.C3 - zbView.Ic4 - zbView.Nc4
            //    - zbView.Ic5 - zbView.Nc5 - zbView.E2 - zbView.C2;
            zbView.C1 = 100 - deal_N2 - deal_He - deal_H2 - deal_C6 - deal_C3 - deal_Ic4 - deal_Nc4 - deal_Ic5 - deal_Nc5 - deal_E2 - deal_C2;


            //1脱硫, 0未脱硫
            decimal pandSSS = Math.Round(Convert.ToDecimal(zbView.H2Smg), 3);

            //录入的硫化氢mg/m3,根据公式计算的值mol%,保留三位小数，如果此值大于0则是脱硫，等于0是未脱硫
            //if (info.Is_desulphurization == 1)  //脱硫
            if (pandSSS > 0)
            {
                //脱硫
                //系数
                decimal xi = (100 - Convert.ToDecimal(zbView.H2Smg)) / 100m;

                zbView.He = xi * zbView.He;
                zbView.H2 = xi * zbView.H2;
                zbView.C6 = xi * zbView.C6;
                zbView.C3 = xi * zbView.C3;

                zbView.Ic4 = xi * zbView.Ic4;
                zbView.Nc4 = xi * zbView.Nc4;
                zbView.Ic5 = xi * zbView.Ic5;
                zbView.Nc5 = xi * zbView.Nc5;

                zbView.E2 = xi * zbView.E2;//二氧化碳 mol
                zbView.C2 = xi * zbView.C2;

                zbView.N2 = xi * zbView.N2;

                //处理小数位，保留三位  //修改时间：2023-1-12 16:38:17
                decimal TTdeal_N2 = Math.Round(Convert.ToDecimal(zbView.N2), 3);
                decimal TTdeal_He = Math.Round(Convert.ToDecimal(zbView.He), 3);
                decimal TTdeal_H2 = Math.Round(Convert.ToDecimal(zbView.H2), 3);
                decimal TTdeal_C6 = Math.Round(Convert.ToDecimal(zbView.C6), 3);

                decimal TTdeal_C3 = Math.Round(Convert.ToDecimal(zbView.C3), 3);
                decimal TTdeal_Ic4 = Math.Round(Convert.ToDecimal(zbView.Ic4), 3);
                decimal TTdeal_Nc4 = Math.Round(Convert.ToDecimal(zbView.Nc4), 3);

                decimal TTdeal_Ic5 = Math.Round(Convert.ToDecimal(zbView.Ic5), 3);
                decimal TTdeal_Nc5 = Math.Round(Convert.ToDecimal(zbView.Nc5), 3);
                decimal TTdeal_E2 = Math.Round(zbView.E2, 3);
                decimal TTdeal_C2 = Math.Round(Convert.ToDecimal(zbView.C2), 3);

                //甲烷 C1（脱硫）
                zbView.C1 = 100 - TTdeal_N2 - TTdeal_He - TTdeal_H2 - TTdeal_C6 - TTdeal_C3 - TTdeal_Ic4 - TTdeal_Nc4 - TTdeal_Ic5
                                        - TTdeal_Nc5 - TTdeal_E2 - TTdeal_C2
                                        - pandSSS;
            }

            //14. 二氧化碳g/m3计算：二氧化碳（mol%）*18.4296
            zbView.CO2 = zbView.E2 * 18.4296m;

            //右下角，压缩因子
            decimal ya_He = -0.01m * Convert.ToDecimal(zbView.He);
            decimal ya_H2 = -0.01m * Convert.ToDecimal(zbView.H2);
            decimal ya_C6 = 0.2907m * Convert.ToDecimal(zbView.C6);
            decimal ya_C3 = 0.1308m * Convert.ToDecimal(zbView.C3);

            decimal ya_iC4 = 0.1673m * Convert.ToDecimal(zbView.Ic4);
            decimal ya_nC4 = 0.1785m * Convert.ToDecimal(zbView.Nc4);
            decimal ya_iC5 = 0.2189m * Convert.ToDecimal(zbView.Ic5);
            decimal ya_NC5 = 0.2295m * Convert.ToDecimal(zbView.Nc5);

            decimal ya_CO2 = 0.0730m * zbView.E2;
            decimal ya_C2 = 0.0895m * Convert.ToDecimal(zbView.C2);
            decimal ya_N2 = 0.0156m * Convert.ToDecimal(zbView.N2);
            decimal ya_C1 = 0.04317m * Convert.ToDecimal(zbView.C1);
            decimal ya_H2S = 0.0898m * Convert.ToDecimal(zbView.H2Smg);

            double sumTall = Convert.ToDouble(ya_He + ya_H2 + ya_C6 + ya_C3 + ya_iC4 + ya_nC4
                + ya_iC5 + ya_NC5 + ya_CO2 + ya_C2 + ya_N2 + ya_C1 + ya_H2S) / 100d;

            decimal ya_yinzi = 1 - Convert.ToDecimal(Math.Pow(sumTall, 2d));
            ya_yinzi = (ya_yinzi * 10000 + 0.5m) / 10000m;

            //相对密度
            decimal cangMd = 28.9626m;
            decimal xd_He = Convert.ToDecimal(zbView.He) * 4.0026m / cangMd;
            decimal xd_H2 = Convert.ToDecimal(zbView.H2) * 2.01588m / cangMd;
            decimal xd_C6 = Convert.ToDecimal(zbView.C6) * 86.17536m / cangMd;
            decimal xd_C3 = Convert.ToDecimal(zbView.C3) * 44.09562m / cangMd;

            decimal xd_iC4 = Convert.ToDecimal(zbView.Ic4) * 58.1222m / cangMd;
            decimal xd_nC4 = Convert.ToDecimal(zbView.Nc4) * 58.1222m / cangMd;
            decimal xd_iC5 = Convert.ToDecimal(zbView.Ic5) * 72.14878m / cangMd;
            decimal xd_nC5 = Convert.ToDecimal(zbView.Nc5) * 72.14878m / cangMd;
            decimal xd_CO2 = zbView.E2 * 44.0095m / cangMd;

            decimal xd_C2 = Convert.ToDecimal(zbView.C2) * 30.06904m / cangMd;
            decimal xd_N2 = Convert.ToDecimal(zbView.N2) * 28.0134m / cangMd;
            decimal xd_C1 = Convert.ToDecimal(zbView.C1) * 16.04246m / cangMd;

            decimal xd_H2S = Convert.ToDecimal(zbView.H2Smg) * 34.08088m / cangMd;
            decimal xd_CAL = (xd_He + xd_H2 + xd_C6 + xd_C3
                                            + xd_iC4 + xd_nC4 + xd_iC5 + xd_nC5 + xd_CO2
                                            + xd_C2 + xd_N2 + xd_C1 + xd_H2S) * 0.999645M / 100m / ya_yinzi;

            //临界温度K
            decimal lintemp_He = Convert.ToDecimal(zbView.He) * 5.195m;
            decimal lintemp_H2 = Convert.ToDecimal(zbView.H2) * 33.26m;
            decimal lintemp_C6 = Convert.ToDecimal(zbView.C6) * 507.5M;
            decimal lintemp_C3 = Convert.ToDecimal(zbView.C3) * 369.89M;
            decimal lintemp_iC4 = Convert.ToDecimal(zbView.Ic4) * 407.81m;

            decimal lintemp_nC4 = Convert.ToDecimal(zbView.Nc4) * 425.13m;
            decimal lintemp_iC5 = Convert.ToDecimal(zbView.Ic5) * 460.35m;
            decimal lintemp_nC5 = Convert.ToDecimal(zbView.Nc5) * 469.7m;
            decimal lintemp_CO2 = zbView.E2 * 304.1m;

            decimal lintemp_C2 = Convert.ToDecimal(zbView.C2) * 305.32m;
            decimal lintemp_N2 = Convert.ToDecimal(zbView.N2) * 126.19m;
            decimal lintemp_C1 = Convert.ToDecimal(zbView.C1) * 190.56m;
            decimal lintemp_H2S = Convert.ToDecimal(zbView.H2Smg) * 373.1m;

            decimal lintemp__CAL = (lintemp_He + lintemp_H2 + lintemp_C6 + lintemp_C3 + lintemp_iC4
                + lintemp_nC4 + lintemp_iC5 + lintemp_nC5 + lintemp_CO2
                + lintemp_C2 + lintemp_N2 + lintemp_C1 + lintemp_H2S) / 100;

            //临界压力 MPa
            decimal linMpa_He = Convert.ToDecimal(zbView.He) * 0.22746m;
            decimal linMpa_H2 = Convert.ToDecimal(zbView.H2) * 1.297m;
            decimal linMpa_C6 = Convert.ToDecimal(zbView.C6) * 3.012M;
            decimal linMpa_C3 = Convert.ToDecimal(zbView.C3) * 4.2512M;
            decimal linMpa_iC4 = Convert.ToDecimal(zbView.Ic4) * 3.629m;

            decimal linMpa_nC4 = Convert.ToDecimal(zbView.Nc4) * 3.796m;
            decimal linMpa_iC5 = Convert.ToDecimal(zbView.Ic5) * 3.378m;
            decimal linMpa_nC5 = Convert.ToDecimal(zbView.Nc5) * 3.370m;
            decimal linMpa_CO2 = zbView.E2 * 7.377m;

            decimal linMpa_C2 = Convert.ToDecimal(zbView.C2) * 4.8722m;
            decimal linMpa_N2 = Convert.ToDecimal(zbView.N2) * 3.396m;
            decimal linMpa_C1 = Convert.ToDecimal(zbView.C1) * 4.599m;
            decimal linMpa_H2S = Convert.ToDecimal(zbView.H2Smg) * 9m;

            decimal linMpa_CAL = (linMpa_He + linMpa_H2 + linMpa_C6 + linMpa_C3 + linMpa_iC4
            + linMpa_nC4 + linMpa_iC5 + linMpa_nC5 + linMpa_CO2
            + linMpa_C2 + linMpa_N2 + linMpa_C1 + linMpa_H2S) / 100;

            //高位发热量 MJ/m³
            decimal quantity_H2 = Convert.ToDecimal(zbView.He) * 285.99m;
            decimal quantity_C6 = Convert.ToDecimal(zbView.C6) * 4196.6m;
            decimal quantity_C3 = Convert.ToDecimal(zbView.C3) * 2220.13m;
            decimal quantity_iC4 = Convert.ToDecimal(zbView.Ic4) * 2869.39m;
            decimal quantity_nC4 = Convert.ToDecimal(zbView.Nc4) * 2878.58m;

            decimal quantity_iC5 = Convert.ToDecimal(zbView.Ic5) * 3530.25m;
            decimal quantity_nC5 = Convert.ToDecimal(zbView.Nc5) * 3537.19m;
            decimal quantity_C2 = Convert.ToDecimal(zbView.C2) * 1561.42m;
            decimal quantity_C1 = Convert.ToDecimal(zbView.C1) * 891.05m;
            decimal quantity_H2S = Convert.ToDecimal(zbView.H2Smg) * 562.19m;

            decimal quantityGaoWeiSum = quantity_H2 + quantity_C6 + quantity_C3 + quantity_iC4
                                                      + quantity_nC4 + quantity_iC5 + quantity_nC5
                                                      + quantity_C2 + quantity_C1 + quantity_H2S;
            decimal quantity_CAL = quantityGaoWeiSum / 100m / ya_yinzi / 24.06m;

            zbView.E9 = ya_yinzi; //压缩因子
            zbView.E10 = quantity_CAL; //高位发热量
            zbView.E11 = xd_CAL; //相对密度
            zbView.E13 = lintemp__CAL; //临界温度K
            zbView.E14 = linMpa_CAL; //临界压力MPa

            //低位发热量。//创建时间：2023-1-11 09:32:15。
            decimal quantity_diwei_num = 44.222m;

            decimal quantity_diwei_H2 = Convert.ToDecimal(zbView.He) * quantity_diwei_num;
            decimal quantity_diwei_C6 = Convert.ToDecimal(zbView.C6) * quantity_diwei_num * 7;
            decimal quantity_diwei_C3 = Convert.ToDecimal(zbView.C3) * quantity_diwei_num * 4;
            decimal quantity_diwei_iC4 = Convert.ToDecimal(zbView.Ic4) * quantity_diwei_num * 5;

            decimal quantity_diwei_nC4 = Convert.ToDecimal(zbView.Nc4) * quantity_diwei_num * 5;
            decimal quantity_diwei_iC5 = Convert.ToDecimal(zbView.Ic5) * quantity_diwei_num * 6;
            decimal quantity_diwei_nC5 = Convert.ToDecimal(zbView.Nc5) * quantity_diwei_num * 6;
            decimal quantity_diwei_C2 = Convert.ToDecimal(zbView.C2) * quantity_diwei_num * 3;

            decimal quantity_diwei_C1 = Convert.ToDecimal(zbView.C1) * quantity_diwei_num * 2;
            decimal quantity_diwei_H2S = Convert.ToDecimal(zbView.H2Smg) * quantity_diwei_num;

            //记和
            decimal quantity_diwei_sum = quantity_diwei_H2 + quantity_diwei_C6 + quantity_diwei_C3 + quantity_diwei_iC4
                                                           + quantity_diwei_nC4 + quantity_diwei_iC5 + quantity_diwei_nC5 + quantity_diwei_C2
                                                           + quantity_diwei_C1 + quantity_diwei_H2S;
            //低位发热量差
            decimal quantity_diwei_cha = (quantityGaoWeiSum - quantity_diwei_sum) / ya_yinzi / 8.3144621m / 293.15m * 101.325m / 100m;

            //低位发热量(MJ/m³)
            zbView.E6 = quantity_diwei_cha.ToString("F3");

            //备注里面，空气占比=录入的氧气（mol%）+空气中的氮
            info.E3 = (info.O2 + N2_step1).ToString();

            info.ViewModel = zbView;

            info.Samping_time = Convert.ToDateTime(info.Samping_time).ToString("yyyy-MM-dd");
            info.Analysis_time = Convert.ToDateTime(info.Analysis_time).ToString("yyyy-MM-dd");
            return info;
        }

        /// <summary>
        /// 编辑一条
        /// </summary>
        /// <param name="gas">气数据</param>
        /// <returns></returns>
        /// 创建时间：2022-12-11 21:47:38。作者：王浩力
        public IResult Edit(Gas_analysis gas)
        {
            //参数检查
            var resultParame = ParamesCheck(gas);
            if (resultParame.CodeEnum != ResultCode.Success)
            {
                return resultParame;
            }

            var info = gas_AnalysisDAL.Get(gas.Id);
            if (info == null)
            {
                return new Result("被编辑的记录不存在");
            }

            //验证编号是否重复  //添加时间：2023-1-10 14:35:41
            if (info.Sample_no != gas.Sample_no)
            {
                bool right = gas_AnalysisDAL.CheckNo(gas.Sample_no);
                if (right)
                {
                    return new Result("样品编号重复了");
                }
            }

            //创建时间：2023-1-10 14:44:57。
            if (gas.All_calc_value < 99 || gas.All_calc_value > 101)
            {
                return new Result("总值范围准许在99~101 ，请检查");
            }

            var result = gas_AnalysisDAL.Update(gas);
            if (result.Code == 200)
            {
                //记录操作日志
                CurrentLoginUser.E12 = 4;
                CurrentLoginUser.E3 = $"编辑天然气信息  ";
                CurrentLoginUser.E1 = gas.Id;
                CurrentLoginUser.E4 = JsonMyHandle.ToJsonStr(info);
                CurrentLoginUser.E5 = JsonMyHandle.ToJsonStr(gas);
                operate_LogDAL.AddOperateLog(CurrentLoginUser);
            }
            return result;
        }

        /// <summary>
        /// 获取气分析记录的操作日志。
        /// </summary>
        /// <param name="gasKey">气分析主键</param>
        /// 创建时间：2022-12-12 11:32:34。作者：王浩力
        public List<Gas_check_history> GetStepLog(long gasKey)
        {
            //校对日志
            var list = gas_Analysis_Upload_LogDAL.GetList(gasKey);
            var info = gas_AnalysisDAL.Get(gasKey);
            //审核日志
            var historyList = gas_Check_HistoryDAL.GetList(gasKey);

            //录入-->校对-->审核
            List<Gas_check_history> backList = new List<Gas_check_history>();
            if (historyList?.Count > 0)
            {
                foreach (var item in historyList)
                {
                    if (item.Check_stat == 1)
                    {
                        item.E3 = "通过";
                    }
                    else
                    {
                        item.E3 = "驳回，" + item.Reject_reason;
                    }
                }
                backList.AddRange(historyList);
            }
            //录入人
            backList.Add(new Gas_check_history()
            {
                Id = info.Id,
                Check_user_name = info.Input_real_name,
                Check_time = info.Input_time,   //操作时间
                E3 = "录入"   //状态
            });
            //校对人
            backList.Add(new Gas_check_history()
            {
                Id = info.Id,
                Check_user_name = info.Proofreader,
                Check_time = info.Proofread_time,   //操作时间
                E3 = "校对"   //状态
            });

            var updateList = list?.Where(g => g.data_version == 2).ToList();
            if (updateList?.Count > 0)
            {
                foreach (var item in updateList)
                {
                    backList.Add(new Gas_check_history()
                    {
                        Id = item.Id,
                        Check_user_name = item.Update_user,
                        Check_time = item.Update_time,   //操作时间
                        E3 = "校对(编辑了数据)"   //状态
                    });
                }
            }
            return backList.OrderBy(g => g.Check_time).ToList();
        }

        /// <summary>
        /// 获取指标数据
        /// </summary>
        /// <param name="zb">指标</param>
        /// <param name="cate">类别</param>
        /// <param name="dateStart">开始日期</param>
        /// <param name="dateEnd">结束日期</param>
        /// <param name="well">井号</param>
        /// <returns></returns>
        /// 创建时间：2022-12-30 18:19:12。作者：王浩力
        public Result<List<PointView>> GetZbPoints(string zb, string cate, string dateStart, string dateEnd, string well)
        {
            //检查日期是否合法
            if (!DateTime.TryParse(dateStart, out DateTime dd1))
            {
                return new Result<List<PointView>>("开始日期无效");
            }
            if (!DateTime.TryParse(dateEnd, out DateTime dd2))
            {
                return new Result<List<PointView>>("结束日期无效");
            }
            if (string.IsNullOrWhiteSpace(well))
            {
                return new Result<List<PointView>>("井号不可为空");
            }
            var list = gas_AnalysisDAL.GetPointCollection(zb, cate, dateStart, dateEnd, well);
            return new Result<List<PointView>>(list) { Code = (int)ResultCode.Success };
        }

        /// <summary>
        /// 根据样本类别统计样本数量，审核通过的，没有被删除的
        /// </summary>
        /// <param name="cate">样本类别</param>
        /// 创建时间：2023-1-3 09:15:58。作者：王浩力
        public int GetCateCount(string cate)
        {
            if (string.IsNullOrWhiteSpace(cate)) { return 0; }
            return gas_AnalysisDAL.GetCateCount(cate);
        }

        /// <summary>
        /// 根据样本类别统计样本数量，审核通过的，没有被删除的
        /// </summary> 
        /// 创建时间：2023-1-3 10:19:34。作者：王浩力
        public int GetOtherCateCount()
        {
            return gas_AnalysisDAL.GetOtherCateCount();
        }

        /// <summary>
        /// 导出样品excel
        /// </summary>
        /// <param name="where"></param>
        /// 创建时间：2023-1-3 16:30:02。作者：王浩力
        public IResult Export(Gas_analysis where)
        {
            string header = @"样品编号,类别,井号,构建名称,顶界层位,底界层位,顶界井深,低界井深,取样位置,取样单位,分析单位,备注,取样条件,委托单位,取样时间,分析时间,取样人姓名,分析人姓名,取样温度,油压,套压,总硫,水露点,He,H2,O2,N2,C1,C6,C3,Ic4,Nc4,Ic5,Nc5,CO2,C2,是否脱硫,H2S(mg),总值,校对人,校对时间,录入人,录入时间,审核人,审核时间";
            string fileName = DateTime.Now.Ticks.ToString();
            int pageSize = 2000;//单个文件导出两千行记录

            //只查询审核通过的记录
            where.WorkList = new List<int>() { 4 };
            try
            {
                var page = gas_AnalysisDAL.Page(where, 1, 1);
                if (page.Total == 0)
                {
                    return new ResultSuccess("没有记录");
                }

                //导出文件数
                int pageNum = (int)Math.Ceiling(page.Total / (decimal)pageSize);

                for (int i = 1; i <= pageNum; i++)
                {
                    var page2 = gas_AnalysisDAL.Page(where, i, pageSize);
                    List<List<object>> rows = new List<List<object>>();
                    foreach (var item in page2.List)
                    {
                        var info = GetInfoStr(item.Id);
                        var cells = new List<object>();
                        cells.Add(item.Sample_no);
                        cells.Add(info.Cate);//类别
                        cells.Add(info.Well_no);
                        cells.Add(info.Build_name);//构建名称
                        cells.Add(info.Top_horizon);//顶界层位
                        cells.Add(info.Bottom_horizon);//底界层位
                        cells.Add(info.Top_well_deep);//顶界井深
                        cells.Add(info.Bottom_well_deep);//低界井深
                        cells.Add(info.Sampling_location);//取样位置
                        cells.Add(info.Samping_company_name);//取样单位
                        cells.Add(info.Analysis_company_name);//分析单位

                        cells.Add(info.Remark);//备注
                        cells.Add(info.Samping_condition);//取样条件
                        cells.Add(info.Client);
                        cells.Add(info.Samping_time);
                        cells.Add(info.Analysis_time);
                        cells.Add(info.Samping_user);
                        cells.Add(info.Analysis_user);
                        cells.Add(info.Samping_temperature);
                        cells.Add(info.Oil_pressure);
                        cells.Add(info.Casing_pressure);
                        cells.Add(info.Zong_s);
                        cells.Add(info.Water_point);

                        cells.Add(info.He);
                        cells.Add(info.H2);
                        cells.Add(info.O2);
                        cells.Add(info.N2);
                        cells.Add(info.C1);
                        cells.Add(info.C6);
                        cells.Add(info.C3);
                        cells.Add(info.Ic4);
                        cells.Add(info.Nc4);
                        cells.Add(info.Ic5);
                        cells.Add(info.Nc5);
                        cells.Add(info.CO2);
                        cells.Add(info.C2);
                        cells.Add(info.Is_desulphurization == 1 ? "脱硫了" : "未脱");
                        cells.Add(info.H2Smg);
                        cells.Add(info.All_calc_value);
                        cells.Add(info.Proofreader);
                        cells.Add(info.Proofread_time);
                        cells.Add(info.Input_real_name);
                        cells.Add(info.Input_time);
                        cells.Add(item.E3);//审核人
                        cells.Add(item.E4);//审核时间

                        rows.Add(cells);
                    }

                    string outPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt", fileName + "_" + i.ToString() + ".csv");
                    CsvHelper.ExportMakeCsv(rows, outPath, "天然气导出" + i, header);

                }

                return new ResultSuccess($"导出成功，共{page.Total}条数据，{pageNum}个文件") { Data = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt") };
            }
            catch (Exception ex)
            {
                return new Result("导出出错：" + ex.Message);
            }

        }

        /// <summary>
        /// 导出样品excel
        /// </summary>
        /// <param name="where"></param>
        /// 创建时间：2023-1-3 16:30:02。作者：王浩力
        public IResult ExportNew(Gas_analysis where)
        {
          string dir=   System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt");
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }

            string header = @"样品编号,类别,井号,构建名称,顶界层位,底界层位,顶界井深,低界井深,取样位置,取样单位,分析单位,备注,取样条件,委托单位,取样时间,分析时间,取样人姓名,分析人姓名,取样温度,油压,套压,总硫,水露点,He,H2,O2,N2,C1,C6,C3,Ic4,Nc4,Ic5,Nc5,CO2,C2,是否脱硫,H2S(mg),总值,校对人,校对时间,录入人,录入时间,审核人,审核时间";

            //打印报告里面，计算的值生成
            header += ",C1(mol%),C2(mol%),C3(mol%),iC4(mol%),nC4(mol%),iC5(mol%),nC5(mol%),C6(mol%),H2S(mol%),CO2(mol%),N2(mol%),He(mol%),H2(mol%)";
            header += ",二氧化碳g/m3,硫化氢mg/m3,总硫mg/m3,水露点(℃),临界温度K,压缩因子,高位发热量(MJ/m³),低位发热量(MJ/m³),相对密度,临界压力(MPa)";

            string fileName = DateTime.Now.Ticks.ToString();
            int pageSize = 2000;//单个文件导出两千行记录

            //只查询审核通过的记录
            where.WorkList = new List<int>() { 4 };
            try
            {
                var page = gas_AnalysisDAL.Page(where, 1, 1);
                if (page.Total == 0)
                {
                    return new ResultSuccess("没有记录");
                }

                //导出文件数
                int pageNum = (int)Math.Ceiling(page.Total / (decimal)pageSize);

                for (int i = 1; i <= pageNum; i++)
                {
                    //每页一个文件，pageSize=2000行， 每个文件两千行
                    var page2 = gas_AnalysisDAL.Page(where, i, pageSize);
                    List<List<object>> rows = new List<List<object>>();

                    //标题
                    List<object> titleList = new List<object>();
                    foreach (var item in header.Split(','))
                    {
                        titleList.Add(item);
                    }
                    rows.Add(titleList);

                    foreach (var item in page2.List)
                    {
                        var info = GetInfoStr(item.Id);
                        var cells = new List<object>();
                        cells.Add(item.Sample_no);
                        cells.Add(info.Cate);//类别
                        cells.Add(info.Well_no);
                        cells.Add(info.Build_name);//构建名称
                        cells.Add(info.Top_horizon);//顶界层位
                        cells.Add(info.Bottom_horizon);//底界层位
                        cells.Add(info.Top_well_deep);//顶界井深
                        cells.Add(info.Bottom_well_deep);//低界井深
                        cells.Add(info.Sampling_location);//取样位置
                        cells.Add(info.Samping_company_name);//取样单位
                        cells.Add(info.Analysis_company_name);//分析单位

                        cells.Add(info.Remark);//备注
                        cells.Add(info.Samping_condition);//取样条件
                        cells.Add(info.Client);
                        cells.Add(info.Samping_time);
                        cells.Add(info.Analysis_time);
                        cells.Add(info.Samping_user);
                        cells.Add(info.Analysis_user);
                        cells.Add(info.Samping_temperature);
                        cells.Add(info.Oil_pressure);
                        cells.Add(info.Casing_pressure);
                        cells.Add(info.Zong_s);
                        cells.Add(info.Water_point);

                        cells.Add(info.He);
                        cells.Add(info.H2);
                        cells.Add(info.O2);
                        cells.Add(info.N2);
                        cells.Add(info.C1);
                        cells.Add(info.C6);
                        cells.Add(info.C3);
                        cells.Add(info.Ic4);
                        cells.Add(info.Nc4);
                        cells.Add(info.Ic5);
                        cells.Add(info.Nc5);
                        cells.Add(info.CO2);
                        cells.Add(info.C2);
                        cells.Add(info.Is_desulphurization == 1 ? "脱硫了" : "未脱");
                        cells.Add(info.H2Smg);
                        cells.Add(info.All_calc_value);
                        cells.Add(info.Proofreader);
                        cells.Add(info.Proofread_time);
                        cells.Add(info.Input_real_name);
                        cells.Add(info.Input_time);
                        cells.Add(item.E3);//审核人
                        cells.Add(item.E4);//审核时间

                        //打印的计算值   2023-2-21 15:26:55
                        var printInfo = info.ViewModel as GasPrintView;
                        cells.Add(printInfo.C1.ToKeepDecimal(3));
                        cells.Add(printInfo.C2.ToKeepDecimal(3));
                        cells.Add(printInfo.C3.ToKeepDecimal(3));
                        cells.Add(printInfo.Ic4.ToKeepDecimal(3));

                        cells.Add(printInfo.Nc4.ToKeepDecimal(3));
                        cells.Add(printInfo.Ic5.ToKeepDecimal(3));
                        cells.Add(printInfo.Nc5.ToKeepDecimal(3));

                        cells.Add(printInfo.C6.ToKeepDecimal(3));
                        cells.Add(printInfo.H2Smg.ToKeepDecimal(3));

                        //10. 二氧化碳
                        cells.Add(printInfo.E2.ToKeepDecimal(3));

                        //11.
                        cells.Add(printInfo.N2.ToKeepDecimal(3));
                        //12.
                        cells.Add(printInfo.He.ToKeepDecimal(3));
                        cells.Add(printInfo.H2.ToKeepDecimal(3));

                        //14.二氧化碳(g/m³)
                        cells.Add(printInfo.CO2.ToKeepDecimal(3));

                        //15.
                        cells.Add(info.H2Smg.ToKeepDecimal(3));
                            
                        //16.总硫
                        cells.Add(info.Zong_s);

                        //17.水露点 (℃)
                        cells.Add(info.Water_point);

                        //18.临界温度(K)
                        cells.Add(printInfo.E13.ToKeepDecimal(3));

                        //19. 压缩因子
                        cells.Add(printInfo.E9.ToKeepDecimal(4));

                        //20.   高位发热量(MJ/m³)
                        cells.Add(printInfo.E10.ToKeepDecimal(3));

                        //21. 低位发热量(MJ/m³)
                        cells.Add(printInfo.E6.ToKeepDecimal(3));

                        //22.  相对密度
                        cells.Add(printInfo.E11.ToKeepDecimal(4));

                        //23.  临界压力(MPa)
                        cells.Add(printInfo.E14.ToKeepDecimal(3));

                        rows.Add(cells);
                    }

                    //string outPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt", fileName + "_" + i.ToString() + ".csv");
                    string outPath = System.IO.Path.Combine(dir, fileName + "_" + i.ToString() + ".xlsx");

                    //CsvHelper.ExportMakeCsv(rows, outPath, "天然气导出" + i, header);
                    ExportExcelHelpter.ExportExcel(rows, outPath);

                }

                return new ResultSuccess($"导出成功，共{page.Total}条数据，{pageNum}个文件") { Data = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt") };
            }
            catch (Exception ex)
            {
                var ex2 = ex.InnerException ?? ex;
                LogHelpter.AddLog($"导出异常{ex2.Message}", null, "export.Error");
                return new Result("导出出错：" + ex.Message);
            }

        }

        /// <summary>
        /// 导出到A2的excel文件
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns></returns>
        /// 创建时间：2023-2-21 17:55:28。作者:王浩力
        public IResult ExportExcelToA2(Gas_analysis where)
        {
            string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "exoprt_A2");
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }

            try
            {
                //只查询审核通过的记录
                where.WorkList = new List<int>() { 4 };
                var page = gas_AnalysisDAL.Page(where, 1, 1);
                if (page.Total == 0)
                {
                    return new ResultSuccess("没有记录");
                }
                //每个文件导出2千条记录
                int pageSize = 2000;

                //导出文件数
                int pageNum = (int)Math.Ceiling(page.Total / (decimal)pageSize);

                ILogin_userDAL login_UserDAL = ServicesHelpter.GetService<ILogin_userDAL>();
                var allUser = login_UserDAL.GetAll_LoginUser();

                for (int i = 1; i <= pageNum; i++)
                {
                    //每页一个文件，pageSize=2000行， 每个文件两千行
                    var page2 = gas_AnalysisDAL.Page(where, i, pageSize);

                    List<List<object>> rows = new List<List<object>>();

                    //标题
                    List<object> titleList = new List<object>();
                    titleList.Add("井号");
                    titleList.Add("取样日期");
                    titleList.Add("取样单位");
                    titleList.Add("取样位置");
                    titleList.Add("取样条件");
                    titleList.Add("送样日期");
                    titleList.Add("分析日期");
                    titleList.Add("分析单位");
                    titleList.Add("硫化氢体积百分比");
                    titleList.Add("一氧化碳体积百分比");
                    titleList.Add("二氧化碳体积百分比");
                    titleList.Add("甲烷体积百分比");
                    titleList.Add("乙烷体积百分比");
                    titleList.Add("丙烷体积百分比");
                    titleList.Add("相对密度");
                    titleList.Add("天然气压缩因子");
                    titleList.Add("分析人");
                    titleList.Add("审核人");
                    titleList.Add("备注");
                    rows.Add(titleList);

                    foreach (var item in page2.List)
                    {
                        //每行数据
                        var info = GetInfoStr(item.Id);
                        List<object> cell_list = new List<object>();
                        cell_list.Add(info.Well_no);

                        //取样日期
                        string dateSamp2 = string.Empty;
                        if (DateTime.TryParse(info.Samping_time, out DateTime dateSamp))
                        {
                            dateSamp2 = dateSamp.ToString("yyyy/MM/dd");
                            cell_list.Add(dateSamp2);
                        }
                        else
                        {
                            cell_list.Add("");
                        }

                        // 取样单位
                        cell_list.Add(info.Samping_company_name);

                        //取样位置              
                        cell_list.Add(info.Sampling_location);

                        //取样条件 
                        cell_list.Add(info.Samping_condition);

                        //送样日期                 
                        cell_list.Add(dateSamp2);

                        //分析日期
                        cell_list.Add(Convert.ToDateTime(info.Analysis_time).ToString("yyyy/MM/dd"));

                        //分析单位
                        cell_list.Add(info.Analysis_company_name);

                        var printInfo = info.ViewModel as GasPrintView;

                        //硫化氢体积百分比
                        cell_list.Add(Math.Round(printInfo.H2Smg??0,3));

                        //一氧化碳体积百分比
                        cell_list.Add(null);

                        //二氧化碳体积百分比
                        cell_list.Add(Math.Round(printInfo.E2,3));

                        //甲烷体积百分比
                        cell_list.Add(Math.Round(printInfo.C1??0,3));

                        //乙烷体积百分比
                        cell_list.Add(Math.Round(printInfo.C2??0,3));

                        //丙烷体积百分比
                        cell_list.Add(Math.Round(printInfo.C3??0,3));

                        //相对密度
                        cell_list.Add(Math.Round(printInfo.E11,4));

                        //天然气压缩因子
                        cell_list.Add(Math.Round( printInfo.E9,4));

                        if (info.Id== 1742152785253765120)
                        {

                        }
                        //分析人
                        if (long.TryParse(info.Analysis_user,out long fxrId))
                        {
                            var analyUser = allUser.FirstOrDefault(g => g.Id == fxrId);
                            cell_list.Add(analyUser?.User_real_name);
                        }
                        else
                        {
                            cell_list.Add(info.Analysis_user);
                        }                      

                        //审核人
                        cell_list.Add(info.E5);

                        //备注
                        cell_list.Add(info.Remark);

                        rows.Add(cell_list);
                    }

                    //生成EXCEL文件
                    string outPath = System.IO.Path.Combine(dir, "Excel_A2_" + DateTime.Now.Ticks + "_" + i.ToString() + ".xlsx");
                    ExportExcelHelpter.ExportExcelA2(rows, outPath);
                }
                return new ResultSuccess($"导出A2成功，共{page.Total}条数据，{pageNum}个文件") { Data = dir };
            }
            catch (Exception ex)
            {
                var ex2 = ex.InnerException ?? ex;
                LogHelpter.AddLog($"导出A2异常{ex2.Message}", null, "export.Error");
                return new Result("导出A2出错：" + ex.Message);
            }
        }

        /// <summary>
        /// 统计有多少个井，返回井列表
        /// </summary>
        /// <returns></returns>
        /// 创建时间：2023-1-18 09:41:42。作者：王浩力
        public List<BaseModel> GetWellNoList()
        {
            return gas_AnalysisDAL.GetWellNoList();
        }

    }
}
